package com.gec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.common.CustomException;
import com.gec.entity.Category;
import com.gec.entity.Dish;
import com.gec.entity.Setmeal;
import com.gec.mapper.CategoryMapper;
import com.gec.service.CategoryService;
import com.gec.service.DishService;
import com.gec.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *
 * </p>
 *
 * @author : [chenxys]
 * @version : [v1.0]
 * @createTime : [2022/7/5 16:39]
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
        @Autowired
        private DishService dishService;
        @Autowired
        private SetmealService setmealService;

    /**
     * 根据ID删除分类
     * @author chenxys
     * @param id 分类ID
     * @return java.lang.Boolean
     */
    @Override
    public void remove(Long id) {
        // 查询当前分类是否关联了菜品,如果已关联则抛出业务异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        // 查询
        int count = dishService.count(dishLambdaQueryWrapper);
        // 已经关联了菜品
        if (count > 0 ){
            // 抛出业务异常
            throw new CustomException("当前分类下关联了菜品,不能删除");
        }

        // 查询当前分类是否关联了套餐,如果已关联则抛出业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        // 查询
        int count1 = setmealService.count(setmealLambdaQueryWrapper);
        // 已关联套餐
        if (count1 > 0){
            // 抛出业务异常
            throw new CustomException("当前分类下关联了套餐,不能删除");
        }
        // 删除分类
        super.removeById(id);
    }
}
